<?php


namespace app\admin\controller;


use app\admin\service\RequestService;
use app\model\SetSystemCate;
use app\Request;

class SetSystemCateController extends AController {


    public function index(RequestService $curdService){
        $list = $curdService->index(new SetSystemCate(), ['parent'], [], false)->each(function ($v){
            $v->parentTitle = getRelValue($v->parent, 'title');
        });
        return jsonSuccess([
            'items' => $list,
        ]);
    }

    public function add(RequestService $curdService){
        return $curdService->add(new SetSystemCate());
    }

    public function edit(RequestService $curdService, Request $request){
        if ($request->isPost()){
            return $curdService->edit(new SetSystemCate());
        }else{
            $id = $request->param('id');
            $info = SetSystemCate::find($id);
            return jsonSuccess([
                'info' => $info
            ]);
        }
    }

    public function del(RequestService $curdService){
        return $curdService->del(new SetSystemCate());
    }


    protected function formatList($list){
        $list = $list->each(function ($v) {
            $v->parentTitle = getRelValue($v->parent, 'title');
        });
        return $list;
    }

    public function options() {

        return jsonSuccess([
            'options' => SetSystemCate::miOptions(),
        ]);
    }


}
